﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyDaiLy.Context;
using QuanLyDaiLy.Model;
using QuanLyDaiLy.Platform;
using QuanLyPhongMach.Model;



namespace QuanLyDaiLy.View
{
    /// <summary>
    /// Interaction logic for WLoaiDaiLy.xaml
    /// </summary>
    public partial class WLoaiDaiLy : Window
    {
        private CLoaiDaiLyContext loaidailyContext;
        private CLoaiDaiLy loaidaily;
        public WLoaiDaiLy()
        {
            InitializeComponent();
            InitializeFeature();
        }

        #region Function

        private void InitializeFeature()
        {
            loaidailyContext = new CLoaiDaiLyContext(CConstant.CONNECTION);
            try
            {
                TaoBangDuLieu();
                TrangThaiMacDinh();
                CapNhatDuLieuBang();
            }
            catch { ;;}
        }

        private void TrangThaiMacDinh()
        {
            txtMaLoaiDaiLy.IsEnabled = false;
            txtTenLoaiDaiLy.IsEnabled = false;
            txtNoToiDa.IsEnabled = false;
            btLuu.IsEnabled = false;
            btXoa.IsEnabled = false;
        }

        private void TrangThaiThem()
        {
            txtTenLoaiDaiLy.IsEnabled = true;
            txtNoToiDa.IsEnabled = true;
            btLuu.IsEnabled = true;
            btXoa.IsEnabled = false;
            txtTenLoaiDaiLy.Clear();
            txtNoToiDa.Clear();
            txtTenLoaiDaiLy.Focus();
            txtMaLoaiDaiLy.Text = loaidailyContext.LayMaSoMoi().ToString();
        }

        private void TrangThaiChon()
        {
            btLuu.IsEnabled = true;
            btXoa.IsEnabled = true;
            txtTenLoaiDaiLy.IsEnabled = true;
            txtNoToiDa.IsEnabled = true;
        }

        private bool KiemTraNhap()
        {
            if (txtTenLoaiDaiLy.Text == "" || txtNoToiDa.Text == "")
            {
                MessageBox.Show("Vui Lòng Nhập Đầy Đủ Thông Tin");
                txtTenLoaiDaiLy.Focus();
                return false;
            }

            return true;
        }

        // chưa luu được 
        private void LuuDaiLy()
        {
            try
            {
                if (KiemTraNhap())
                {

                    if (Convert.ToInt32(txtMaLoaiDaiLy.Text) < loaidailyContext.LayMaSoMoi())
                    {
                        var result = MessageBox.Show("Bạn Có Chắc Chắn Sửa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.Yes)
                        {
                            loaidaily = new CLoaiDaiLy(int.Parse(txtMaLoaiDaiLy.Text),txtTenLoaiDaiLy.Text, Convert.ToUInt32(txtNoToiDa.Text));
                            loaidailyContext.CapNhat(loaidaily);
                            MessageBox.Show("Sửa Thành Công");
                        }
                        TrangThaiChon();

                    }
                    else
                    {
                        loaidaily = new CLoaiDaiLy((long.Parse)(txtMaLoaiDaiLy.Text), txtTenLoaiDaiLy.Text, (long.Parse)(txtNoToiDa.Text));
                        loaidailyContext.Them(loaidaily);
                        MessageBox.Show("Lưu Thành Công");
                        TrangThaiThem();
                    }

                    CapNhatDuLieuBang();
                }
            }
            catch { }
                
        }

        private void XoaDaiLy()
        {
            try
            {
                var result = MessageBox.Show("Bạn Có Chắc Chắn Xóa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    loaidailyContext.Xoa(Convert.ToInt32(txtMaLoaiDaiLy.Text));
                    CapNhatDuLieuBang();
                    TrangThaiMacDinh();  
                    MessageBox.Show("Xóa Thành Công");                 
                }
            }
            catch { MessageBox.Show("Chưa Thể Xóa"); }
        }

        private void ChonDaiLy()
        {
            try
            {
                TrangThaiChon();
                if (lvLoaiDaiLy.SelectedItem is CLoaiDaiLy)
                {
                    txtMaLoaiDaiLy.Text = (lvLoaiDaiLy.SelectedItem as CLoaiDaiLy).MaLoaiDaiLy.ToString();
                    txtTenLoaiDaiLy.Text = (lvLoaiDaiLy.SelectedItem as CLoaiDaiLy).TenLoaiDaiLy;
                    txtNoToiDa.Text = (lvLoaiDaiLy.SelectedItem as CLoaiDaiLy).NoToiDa.ToString();
                }
            }
            catch { }
        }

        private void TaoBangDuLieu()
        {
            try
            {
                GridView grdView = new GridView();
                grdView.AllowsColumnReorder = true;
                grdView.ColumnHeaderToolTip = "LOAIDAILY";

                GridViewColumn maLoaiDaiLy = new GridViewColumn();
                maLoaiDaiLy.DisplayMemberBinding = new Binding("MaLoaiDaiLy");
                maLoaiDaiLy.Header = "Mã Loại Đại Lý";
                maLoaiDaiLy.Width = 120;
                grdView.Columns.Add(maLoaiDaiLy);

                GridViewColumn tenloaidaily = new GridViewColumn();
                tenloaidaily.DisplayMemberBinding = new Binding("TenLoaiDaiLy");
                tenloaidaily.Header = "Họ Tên";
                tenloaidaily.Width = 120;
                grdView.Columns.Add(tenloaidaily);

                GridViewColumn notoida = new GridViewColumn();
                notoida.DisplayMemberBinding = new Binding("NoToiDa");
                notoida.Header = " Nợ Tối Đa";
                notoida.Width = 120;
                grdView.Columns.Add(notoida);

                lvLoaiDaiLy.View = grdView;
            }
            catch { }
        }

        private void CapNhatDuLieuBang()
        {
            lvLoaiDaiLy.ItemsSource = loaidailyContext.LayDanhSachTatCa();
        }

        #endregion

        #region EVEN

        private void txtNoToiDa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LuuDaiLy();
            }
        }


        private void btThemDaiLy_Click(object sender, RoutedEventArgs e)
        {
            TrangThaiThem();
        }

        private void btLuu_Click(object sender, RoutedEventArgs e)
        {
            LuuDaiLy();
        }

        private void btXoa_Click(object sender, RoutedEventArgs e)
        {
            XoaDaiLy();
        }
        
        private void lvLoaiDaiLy_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ChonDaiLy();
        }



        #endregion
    }
}
